(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

open XNum
open Logging
open Dim
open Box
  
type direction = LR | RL


(* The maximal badness tolerated by make. *)
let max_h_badness = ref (num_of_int 1000)
  

(* h-boxes *)

(* |dimensions <boxes>| returns the dimensions of an h-box containing <boxes>. *)
let dimensions boxes =
  let rec iter width height depth boxes =
    match boxes with
      | [] -> ((xdim_to_dim width), height, depth)
      | b :: bs ->
          iter (xdim_add_dim width b.b_width) (dim_max height b.b_height)
            (dim_max depth b.b_depth) bs
  in
    iter xdim_zero dim_zero dim_zero boxes
  
let calc_xwidth boxes =
  let rec iter w boxes =
    match boxes with
      | [] -> w
      | b :: bs -> iter (xdim_add_dim w b.b_width) bs
  in
    iter xdim_zero boxes
  
let calc_width boxes = xdim_to_dim (calc_xwidth boxes)
  

(* |calc_width_and_glue <boxes>| calculates the total width
   of <boxes>. Additionally, it returns a list of all intervals
   (a,b) which contain glue. If these intervals are
   (a_1,b_1),...,(a_n,b_n) and the total width is w then the returned
   list contains the values

     w - b_n
     b_n - a_n
     a_n - b_n-1
     b_n-1 - a_n-1
     ...
     a_2 - b_1
     b_1 - a_1
     a_1
*)
let calc_width_and_glue boxes =
  let rec iter glue width delta boxes =
    match boxes with
      | [] -> (width, (delta :: glue))
      | b :: bs ->
          let new_width = xdim_add_dim width b.b_width in
            (* Check whether it is explicit glue. *)
            (match b.b_contents with
               | GlueBox (false, _) ->
                   iter ((dim_to_xdim b.b_width) :: delta :: glue) new_width
                     xdim_zero bs
               | _ -> iter glue new_width (xdim_add_dim delta b.b_width) bs)
  in
    iter [] xdim_zero xdim_zero boxes
  

(* |layout <direction> <boxes>| computes the positions of the boxes and puts them into a
   hbox in left-right order. *)
let layout dir boxes =
  match boxes with
    | [] -> empty_box
    | [b] ->
        begin
          match b.b_contents with
            | CommandBox (`GfxCmd c) -> new_compound_box dim_zero dim_zero dim_zero [c]
            | _ -> b
        end
    | _ ->
        let (width, height, depth) = dimensions boxes in
        let result = ListBuilder.make () in
        let start_x =
          begin
            match dir with
              | LR -> xdim_zero
              | RL -> dim_to_xdim width
          end
        in
        let rec iter x boxes =
          match boxes with
            | [] -> new_compound_box width height depth (ListBuilder.get result)
            | b :: bs ->
                match b.b_contents with
                  | CommandBox (`GfxCmd c) ->
                      ListBuilder.add result c;
                      iter x bs
                  | _ ->
                      match dir with
                        | LR ->
                            ListBuilder.add result
                              (Graphic.PutBox (xdim_select_order x width.d_stretch_order width.d_shrink_order, dim_zero, b));
                            iter (xdim_add_dim x b.b_width) bs
                        | RL ->
                            let new_x = xdim_sub_dim x b.b_width in
                              ListBuilder.add result
                                (Graphic.PutBox (xdim_select_order new_x width.d_stretch_order width.d_shrink_order, dim_zero, b));
                              iter new_x bs
        in
          iter start_x boxes
  

let layout_scaled dir (orig_width, height, depth) scaled_width boxes =
  let (factor, order) = adjustment_ratio orig_width scaled_width in
  let ratio =
    if lt_num factor (num_of_int (-1)) && order = 0 then
      ((num_of_int (-1)), 0)
    else
      (factor, order)
  in
  let bad = dim_scale_badness (factor, order) in
    if gt_num bad !max_h_badness then
      begin
        if lt_num factor num_zero then
          log_string "\nWarning: Overfull hbox (badness "
        else
          log_string "\nWarning: Underfull hbox (badness ";
        if lt_num bad infinite then
          log_num bad
        else
          log_string "infinite";
        log_string ")!\n";
        List.iter log_box boxes;
        log_string "\n"
      end
    else
      ();
    let result = ListBuilder.make () in
    let start_x =
      match dir with
        | LR -> xdim_zero
        | RL -> dim_to_xdim (fixed_dim scaled_width) in
    let rec iter x boxes =
      match boxes with
        | [] ->
            new_compound_box (fixed_dim scaled_width) height depth
              (ListBuilder.get result)
        | b :: bs ->
            match b.b_contents with
              | CommandBox (`GfxCmd c) -> (ListBuilder.add result c; iter x bs)
              | _ ->
                  match dir with
                    | LR ->
                        let box = scale_box_horiz b ratio
                        in
                          ListBuilder.add result
                            (Graphic.PutBox (fixed_dim x.xd_base, dim_zero,
                                             box));
                          iter (xdim_add_dim x box.b_width) bs
                    | RL ->
                        let box = scale_box_horiz b ratio in
                        let new_x = xdim_sub_dim x box.b_width in
                          ListBuilder.add result
                            (Graphic.PutBox (fixed_dim new_x.xd_base, dim_zero,
                                             box));
                          iter new_x bs
    in
      iter start_x boxes

  
let make dir boxes = layout dir boxes
  
let make_to dir width boxes =
  layout_scaled dir (dimensions boxes) width boxes
  
let make_scaled dir factor boxes =
  let (w, h, d) = dimensions boxes in
    layout_scaled dir (w, h, d) (mult_num factor w.d_base) boxes
  
let make_spread dir amount boxes =
  let (w, h, d) = dimensions boxes in
    layout_scaled dir (w, h, d) (add_num w.d_base amount) boxes
